package com.arrays.a1;

import java.util.Arrays;

//移除元素27
public class RemoveElement27 {

    public static int method(int[] nums,int val){
        int len=nums.length;
        int l=0;
        for (int r=0;r<len;r++){
            if (nums[r]!=val){
                nums[l]=nums[r];
                l++;
            }
        }
        System.out.println(Arrays.toString(nums));
        return l;
    }

    //遍历将与目标值不同的放入另一个数组
    public static int remove(int[] nums, int val) {
        int len=nums.length;
        int[] res=new int[len];
        int total=0;
        for(int i=0;i<len;i++ ){
            if(nums[i]!=val){
                    res[i]=nums[i];
            }
            if(res[i]!=0){
                total++;
            }
        }
        System.out.println(Arrays.toString(res));//[0, 2, 2, 0]
        return total;
    }

    public static void main(String[] args) {
        int[] a={3,2,2,3};
        int re = remove(a, 3);
        System.out.println(re);
    }
}
